package com.ruoyi.web.controller.miniprogram;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.MiniProgramUserDto;
import com.ruoyi.common.core.domain.entity.miniprogram.MiniAddress;
import com.ruoyi.common.core.domain.entity.system.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.miniprogram.service.IMiniprogramUserService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信小程序用户。
 */
@RestController
@RequestMapping("/miniprogram/v1/user")
public class MiniUserController extends BaseController {

    @Autowired
    private IMiniprogramUserService miniprogramUserService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 退出登录。
     *
     * @return
     */
    @PostMapping("/logout")
    public AjaxResult logout() {
        miniprogramUserService.logout();
        return success("退出成功");
    }

    /**
     * 获取用户信息。
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo() {
        Long userId = SecurityUtils.getLoginUser().getUserId();

        SysUser user = sysUserService.selectUserById(userId);
        MiniProgramUserDto programUserDto = new MiniProgramUserDto();
        BeanUtils.copyProperties(user, programUserDto);

        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", programUserDto);
        return ajax;
    }

    /**
     * 修改用户信息。
     *
     * @return
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody MiniProgramUserDto userDto) {
        int row = miniprogramUserService.updateMiniprogramUser(userDto);
        return toAjax(row);
    }

    @PostMapping("/addAddress")
    public AjaxResult addAddress(@RequestBody MiniAddress address) {
        return toAjax(miniprogramUserService.insertAddress(address));
    }

    /**
     * 更新地址信息。
     *
     * @param address
     * @return
     */
    @PostMapping("/updateAddress")
    public AjaxResult updateAddress(@RequestBody MiniAddress address) {
        return toAjax(miniprogramUserService.updateAddress(address));
    }

    /**
     * 根据地址id查询当前用户的单个地址信息。
     *
     * @param addressId
     * @return
     */
    @GetMapping("/getAddressInfo/{addressId}")
    public AjaxResult getAddressInfo(@PathVariable("addressId") Long addressId) {
        MiniAddress address = miniprogramUserService.getAddressInfo(addressId);
        return success(address);
    }

    /**
     * 删除指定地址。
     *
     * @param addressId
     * @return
     */
    @DeleteMapping("/deleteAddress/{addressId}")
    public AjaxResult deleteAddress(@PathVariable("addressId") Long addressId) {
        return toAjax(miniprogramUserService.deleteAddress(addressId));
    }

    @GetMapping("/getAddressList")
    public AjaxResult getAddressList() {
        List<MiniAddress> addressList = miniprogramUserService.getAddressList();
        return success(addressList);
    }



}
